﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.Net;
using System.Windows.Forms;

namespace PDAMain
{
    public class GPRS
    {
        public OpenNETCF.Net.ConnectionManager ConnMgr = new OpenNETCF.Net.ConnectionManager();
        private DestinationInfoCollection cenum;
        private bool is_connected;

        /// <summary>
        /// 打开/关闭指定GPRS连接
        /// </summary>
        /// <param name="connect">true:打开，false:关闭</param>
        /// <param name="_Description">连接名称：Internet 设置(默认)</param>
        public void ConnGPRS(bool connect,string _Description)
        {
            long startTime;
            cenum = ConnMgr.EnumDestinations();
            is_connected = false;
            try
            {
                foreach (DestinationInfo em in cenum)
                {
                    if (em.Description == _Description)
                    {
                        if (connect)
                        {
                            ConnMgr.Connect(em.Guid, true, ConnectionMode.Synchronous); ;
                            startTime = DateTime.Now.Ticks;
                            while ((!is_connected) && (DateTime.Now.Ticks - startTime <  TimeSpan.TicksPerSecond)) ;
                        }
                        else
                        {
                            ConnMgr.RequestDisconnect();
                        }
                    }
                }
            }
            catch(SystemException err)
            {
                //MessageBox.Show("err 700"+err.Message);
            }
        }

        /// <summary>
        /// 打开/关闭GPRS连接
        /// </summary>
        /// <param name="connect">true:打开，false:关闭</param>
        public void ConnGPRS(bool connect)
        {
            long startTime;
            cenum = ConnMgr.EnumDestinations();
            is_connected = false;
            string tmp = "";
            try
            {
                foreach (DestinationInfo em in cenum)
                {
                    if (connect)
                        tmp = "Connected";
                    else
                        tmp = "Disconnected";
                    if (GPRS_Status() == tmp)
                        break;
                    else
                    {
                        if (connect)
                        {
                            ConnMgr.Connect(em.Guid, true, ConnectionMode.Synchronous); ;
                            startTime = DateTime.Now.Ticks;
                            while ((!is_connected) && (DateTime.Now.Ticks - startTime < TimeSpan.TicksPerSecond)) ;
                        }
                        else
                        {
                            ConnMgr.RequestDisconnect();
                        }
                    }
                }
            }
            catch (SystemException err)
            {
                //MessageBox.Show("err 700" + err.Message);
            }
        }

        /// <summary>
        /// 获取GPRS连接状态
        /// </summary>
        /// <returns></returns>
        public string GPRS_Status()
        {
            return ConnMgr.Status.ToString();
        }
    }
}
